/*************************************************************
 * File: SpringJaxRsApplication.java
 * Copyright (c) 2012
 * Author: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Sep 8, 2012
 * Description: 
 ************************************************************/

package snow.core.web.rest.restlet;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restlet.Component;
import org.restlet.ext.jaxrs.JaxRsApplication;
import org.restlet.ext.jaxrs.JaxRsRestlet;
import org.springframework.beans.factory.InitializingBean;

/**
 * 支持Spring注入的JaxRsApplication
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public class SpringJaxRsApplication extends JaxRsApplication implements InitializingBean {

	private final Log log = LogFactory.getLog(getClass());

	private Component component;
	private List<Object> providers;

	public void afterPropertiesSet() throws Exception {
		log.info("Start Execute afterPropertiesSet Method...");
		try {
			log.info("Execute Super's method setContext...");
			super.setContext(component.getContext().createChildContext());
			if (CollectionUtils.isNotEmpty(this.providers)) {
				JaxRsRestlet restlet = super.getJaxRsRestlet();
				for (final Object provider : this.providers) {
					if (provider != null) {
						restlet.addSingleton(provider);
					}
				}
			}
			this.component.getDefaultHost().attach(this);
		} finally {
			log.info("Close Execute afterPropertiesSet Method...");
		}
	}

	public Component getComponent() {
		return component;
	}

	public List<Object> getProviders() {
		return this.providers;
	}

	public void setComponent(Component component) {
		this.component = component;
	}

	public void setProviders(List<Object> providers) {
		this.providers = providers;
	}
}
